// pages/home/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlatform: 0,
    param: '',

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    var windowHeight = wx.getSystemInfoSync().windowHeight;
    windowHeight = (windowHeight - 150) / 2;
    that.setData({
      windowHeight: windowHeight
    })
    if (options) {
      that.data.param = options;
      if (options.id) {
        wx.setStorageSync('cardId', options.id)
      }
      if (options.isPlatform) {
        that.data.isPlatform = options.isPlatform;
      }
    }
  },

  getInfo: function () {
    let that = this;
    let cardId = wx.getStorageSync('cardId');
    if (cardId) {
      that.getStaffTabbars(cardId, 0);
      // wx.request({
      //   url: app.globalData.host + '/api/staff/getInfo?id=' + cardId,
      //   timeout: 3000,
      //   success: function (res) {
      //     if (res.data.Code == 0) {
      //       that.getStaffTabbars(cardId, res.data.Data.Uid);
      //     } else {
      //       wx.reLaunch({
      //         url: '/pages/indexDefault/index',
      //       })
      //     }
      //   },
      //   fail: function (error) {
      //     wx.showToast({
      //       title: '网络繁忙',
      //       mask: true
      //     })
      //     setTimeout(function () {
      //       wx.reLaunch({
      //         url: '/pages/indexDefault/index',
      //       })
      //     }, 1500)
      //   }
      // })
    } else {
      that.getStaffIdByUid(function (staffId) {
        if (staffId > 0) {
          wx.setStorageSync("cardId", staffId);
          wx.reLaunch({
            url: '/pages/index/index?id=' + staffId,
          })
        } else {
          wx.reLaunch({
            url: '/pages/indexDefault/index',
          })
        }
      });
    }
  },

  getStaffTabbars: function (cardId, createUid) {
    let that = this;
    var TabbarList = [{
      id: 0,
      pagePath: '/pages/index/index',
      text: '首页',
      iconPath: '/images/tabbar/20.png',
      selectedIconPath: '/images/tabbar/19.png',
      iCount: 0,
      isActive: true,
      target: 'relaunch'
    },
    {
      id: 1,
      pagePath: '/pages/classes-new/index',
      text: '产品',
      iconPath: '/images/tabbar/22.png',
      selectedIconPath: '/images/tabbar/21.png',
      iCount: 0,
      isActive: false,
      target: 'relaunch'
    },
    {
      id: 2,
      pagePath: '/merchant/merchants/nearbyCoupon/index',
      text: '9块9',
      iconPath: '/images/tabbar/32.png',
      selectedIconPath: '/images/tabbar/31.png',
      iCount: 0,
      isActive: false,
      target: 'relaunch'
    },
    {
      id: 3,
      pagePath: '/pages/about/index',
      text: '官网',
      iconPath: '/images/tabbar/26.png',
      selectedIconPath: '/images/tabbar/25.png',
      iCount: 0,
      isActive: false,
      target: 'relaunch'
    },
    {
      id: 4,
      pagePath: '/pages/weijieke/states/index',
      text: '商圈',
      iconPath: '/images/tabbar/24.png',
      selectedIconPath: '/images/tabbar/23.png',
      iCount: 0,
      isActive: false,
      target: 'relaunch'
    },
    {
      id: 5,
      pagePath: '/merchant/merchants/myCardCoupon/index',
      text: '我的',
      iconPath: '/images/tabbar/29.png',
      selectedIconPath: '/images/tabbar/30.png',
      iCount: 0,
      isActive: false,
      target: 'relaunch'
    }
    ]
    wx.request({
      url: app.globalData.host + '/api/AddressBook/GetStaffTabbars?staffId=' + cardId + '&uid=' + createUid,
      timeout: 3000,
      success: function (res) {
        if (res.data.Code == 0) {
          let list = res.data.Data;
          for (let i = 0; i < list.length; i++) {
            if (app.globalData.userInfo && createUid == app.globalData.userInfo.Uid) {//自己
              wx.reLaunch({
                url: '/pages/index/index?isPlatform=' + that.data.isPlatform,
              })
            } else {
              if (list[0].Display) {
                wx.setStorageSync('showIndex', TabbarList[0].pagePath);
              } else if (list[1].Display) {
                wx.setStorageSync('showIndex', TabbarList[1].pagePath);
              } else if (list[2].Display) {
                wx.setStorageSync('showIndex', TabbarList[2].pagePath);
              } else if (list[3].Display) {
                wx.setStorageSync('showIndex', TabbarList[3].pagePath);
              } else if (list[4].Display) {
                wx.setStorageSync('showIndex', TabbarList[4].pagePath);
              } else {
                wx.setStorageSync('showIndex', TabbarList[5].pagePath);
              }
              TabbarList[i].display = list[i].Display;
              if (TabbarList[i].display) {
                wx.reLaunch({
                  url: TabbarList[i].pagePath + '?isPlatform=' + that.data.isPlatform,
                })
                break;
              }
            }
          }
        }
      },
      fail: function (error) {
        wx.showToast({
          title: '网络繁忙',
          mask: true
        })
        setTimeout(function(){
          wx.reLaunch({
            url: '/pages/indexDefault/index',
          })
        },1500)
      }
    })
  },

  //判断是否创建过名片
  getStaffIdByUid: function (cb) {
    let staffid = 0;
    wx.request({
      url: app.globalData.host + '/api/Staff/GetStaffIdByUid?Uid=' + app.globalData.userInfo.Uid,
      timeout: 3000,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data) {
            staffid = res.data.Data.Id
          }
          cb(staffid);
        }
      },
      fail: function (err) {
        wx.showToast({
          title: '网络繁忙',
          mask: true
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/indexDefault/index',
          })
        }, 1500)
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    if (app.globalData.userInfo) {
      that.getInfo();
    } else {
      setTimeout(function () {
        this.onShow();
      }.bind(that), 1000);
    }
  }
})